package com.bocloud.cmp.server.controller.app.resource;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bocloud.cmp.entity.app.resource.OpenshiftCluster;
import com.bocloud.cmp.interfaces.app.resource.OpenshiftClusterService;
import com.bocloud.common.model.BsmResult;
import com.bocloud.common.model.Param;
import com.bocloud.common.model.RequestUser;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.JSONTools;
import com.bocloud.common.utils.ResultTools;

/**
 * @author Zaney
 * @data:2017年3月28日
 * @describe:openshift集群server控制层
 */
@RestController
@RequestMapping("/openshift/cluster")
public class OpenshiftClusterController {
	private final OpenshiftClusterService openshiftClusterService;

	@Autowired
	public OpenshiftClusterController(OpenshiftClusterService openshiftClusterService) {
		super();
		this.openshiftClusterService = openshiftClusterService;
	}

	/**
	 * 创建openshift集群
	 * 
	 * @param params
	 * @param user
	 * @return
	 */
	@RequestMapping("/create")
	public BsmResult create(@RequestParam(value = Common.PARAMS, required = true) String params,
			@Value(value = Common.REQ_USER) RequestUser user) {
		JSONObject object = JSONTools.isJSONObj(params);
		if (null != object) {
			OpenshiftCluster cluster = JSONObject.parseObject(object.toJSONString(), OpenshiftCluster.class);
			return openshiftClusterService.create(user.getId(), cluster);
		} else {
			return ResultTools.formatErrResult();
		}
	}

	/**
	 * 统计openshift集群的个数
	 * 
	 * @return
	 */
	@RequestMapping("/count")
	public BsmResult getTotal() {
		return openshiftClusterService.getTotal();
	}

	/**
	 * 列表
	 * 
	 * @param page
	 *            当前页码
	 * @param rows
	 *            页面数据大小
	 * @param params
	 *            查询参数，例如：[{"param":{"name":"aaa","password":"1245"},"sign":
	 *            "EQ|UEQ"},{"param":{"name":"aaa","password":"1245"},"sign":
	 *            "EQ|UEQ"}]
	 * @param sorter
	 *            排序参数，例如：{"name":0|1,"password":0|1},0表示增序，1表示降序
	 * @param simple
	 *            简单查询标记，只有true和false,为false时返回用户的详细信息，为true时只返回id和name值。
	 * @param user
	 *            操作者信息
	 * @return 数据结果集
	 */
	@RequestMapping(value = "/list", method = { RequestMethod.POST })
	public BsmResult list(@RequestParam(value = Common.PAGE, required = false) Integer page,
			@RequestParam(value = Common.ROWS, required = false) Integer rows,
			@RequestParam(value = Common.PARAMS, required = false) String params,
			@RequestParam(value = Common.SORTER, required = false) String sorter,
			@RequestParam(value = Common.SIMPLE, required = false) Boolean simple,
			@Value(Common.REQ_USER) RequestUser user) {
		List<Param> paramList = JSONObject.parseArray(params, Param.class);
		@SuppressWarnings("unchecked")
		Map<String, String> sorterMap = JSONObject.parseObject(sorter, HashMap.class);
		return openshiftClusterService.list(page, rows, paramList, sorterMap, simple);
	}

	/**
	 * 获取openshift集群详情
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/{id}/detail", method = { RequestMethod.GET })
	public BsmResult detail(@PathVariable(value = Common.ID) Long id) {
		return openshiftClusterService.detail(id);
	}

	/**
	 * 修改openshift集群信息
	 * 
	 * @param params
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "/{id}/modify", method = { RequestMethod.POST })
	public BsmResult modify(@RequestParam(value = Common.PARAMS, required = true) String params,
			@PathVariable(value = Common.ID) Long id, @Value(Common.REQ_USER) RequestUser user) {
		JSONObject object = JSONTools.isJSONObj(params);
		if (null != object) {
			OpenshiftCluster cluster = JSONObject.parseObject(object.toJSONString(), OpenshiftCluster.class);
			return openshiftClusterService.modify(cluster, user.getId());
		} else {
			return ResultTools.formatErrResult();
		}
	}

	/**
	 * 删除
	 * 
	 * @param params
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "/remove", method = { RequestMethod.POST })
	public BsmResult remove(@RequestParam(value = Common.PARAMS, required = true) String params,
			@Value(Common.REQ_USER) RequestUser user) {
		JSONObject jsonObject = JSONTools.isJSONObj(params);
		List<Long> ids = JSON.parseArray(jsonObject.getString("ids"), Long.class);
		return openshiftClusterService.remove(ids, user.getId());
	}

	/**
	 * 按照应用查询
	 * @author zjm
	 * @date 2017年4月9日
	 *
	 * @param params
	 * @param appId
	 * @return
	 */
	@RequestMapping(value = "/listOpenshiftClusterByAppId", method = { RequestMethod.POST })
	public BsmResult listOpenshiftClusterByAppId(@RequestParam(value = Common.PARAMS, required = false) String params,
			@RequestParam(value = "appId", required = true) Long appId) {
		return openshiftClusterService.listOpenshiftClusterByAppId(appId);
	}
	
	/**
	 * master namespace、username校验
	 * @param params
	 * @return
	 */
	@RequestMapping(value = "/master/check", method = {RequestMethod.POST })
	public BsmResult checkMaster(@RequestParam(value = Common.PARAMS, required = false) String params){
		JSONObject object = JSONTools.isJSONObj(params);
		if (null != object) {
			OpenshiftCluster cluster = JSONObject.parseObject(object.toJSONString(), OpenshiftCluster.class);
			return openshiftClusterService.checkMaster(cluster);
		}else{
			return ResultTools.formatErrResult();
		}
	}
	
	/**
	 * 添加节点
	 * @param params
	 * @return
	 */
	@RequestMapping(value = "/create/node", method = { RequestMethod.POST })
	public BsmResult addNode(@RequestParam(value = Common.PARAMS, required = true) String params) {
		JSONObject jsonObject = JSONTools.isJSONObj(params);
		if (null != jsonObject) {
			String newNodeType = jsonObject.getString("nodeType");
			String newNodeName = jsonObject.getString("nodeName");
			String newNodeLabel = jsonObject.getString("nodeLabel");
			Long clusterId = Long.valueOf(jsonObject.getString("clusterId"));
			return openshiftClusterService.addNode(newNodeType, newNodeName, newNodeLabel, clusterId);
		} else {
			return ResultTools.formatErrResult();
		}
	}
	
	/**
	 * 移除节点
	 * @param params
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/node", method = { RequestMethod.DELETE })
	public BsmResult delNode(@RequestParam(value = Common.PARAMS, required = true) String params) {
		JSONObject jsonObject = JSONTools.isJSONObj(params);
		if (null != jsonObject) {
			Long clusterId = Long.valueOf(jsonObject.getString("clusterId"));
			String nodeName = jsonObject.getString("nodeName");
			return openshiftClusterService.delNode(nodeName, clusterId);
		}else{
			return ResultTools.formatErrResult();
		}
	}
}
